package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.seu.herald.informationplatform.bean.league.VoteResult;

public class VoteResultDataAccess extends DataAccessObject {
	
	private static VoteResultDataAccess _instance;
	
	public static synchronized VoteResultDataAccess getInstance() {
		if (_instance == null) {
			_instance = new VoteResultDataAccess();
		}
		return _instance;
	}
	
	protected VoteResultDataAccess() {}
	
	private static final String STATEMENT_CREATE_NEW_VOTE_RESULT =
			"INSERT INTO lg_vote_result (user_id, vote_item_id) VALUES (?, ?)";
	
	public void createNewVoteResult(VoteResult voteResult)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_CREATE_NEW_VOTE_RESULT);
			ps.setInt(1, voteResult.getUserId());
			ps.setInt(2, voteResult.getVoteItem().getId());
			
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}
	
	public void createNewVoteResult(int userId, int voteItemId)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_CREATE_NEW_VOTE_RESULT);
			ps.setInt(1, userId);
			ps.setInt(2, voteItemId);
			
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_CONTAINS =
			"SELECT * FROM lg_vote_result " +
			"WHERE user_id=? AND vote_item_id=? LIMIT 1;";
	
	public boolean contains(int userId, int voteItemId) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_CONTAINS);
			ps.setInt(1, userId);
			ps.setInt(2, voteItemId);
			
			ResultSet rs = ps.executeQuery();
			return rs.next();
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_RETRIEVE_VOTE_RESULT_NUMBER =
			"SELECT COUNT(*) FROM lg_vote_item, lg_vote_result " +
			"WHERE lg_vote_result.user_id=? " +
			"AND lg_vote_item.vote_id=? " +
			"AND lg_vote_item.id=lg_vote_result.vote_item_id;";
	
	public int getVoteResultNumber(int userId, int voteId)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_RETRIEVE_VOTE_RESULT_NUMBER);
			ps.setInt(1, userId);
			ps.setInt(2, voteId);
			
			ResultSet rs = ps.executeQuery();
			if (!rs.next()) {
				return -1;
			}
			
			return rs.getInt(1);
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_GET_VOTE_NUM_BY_VOTE_ITEM_ID =
			"SELECT COUNT(*) FROM lg_vote_result WHERE vote_item_id=?;";
	
	public int getVoteResultNumByVoteItemId(int voteItemId)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_GET_VOTE_NUM_BY_VOTE_ITEM_ID);
			ps.setInt(1, voteItemId);
			
			ResultSet rs = ps.executeQuery();
			if (!rs.next()) {
				return -1;
			}
			
			return rs.getInt(1);
		} finally {
			connection.close();
		}
	}
	
	private static final String
		STATEMENT_DELETE_VOTE_RESULTS_BY_VOTE_ITEM_ID =
			"DELETE FROM lg_vote_result WHERE vote_item_id=?;";
	
	public void deleteAllVoteResultsByVoteItemId(int voteItemId)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_DELETE_VOTE_RESULTS_BY_VOTE_ITEM_ID);
			ps.setInt(1, voteItemId);
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}

}
